//
//  NXSharedView.m
//  NCube
//
//  Created by kepuna on 2017/10/13.
//  Copyright © 2017年 junjie.liu. All rights reserved.
//

#import "NXSharedView.h"
#import "NXSharedButton.h"
#import "UIView+NXAdd.h"
#import "UIImage+NXAdd.h"
#import "NXSharedModel.h"

#define RGB(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define RGBA(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
#define ScreenWidth  [UIScreen mainScreen].bounds.size.width
#define kScale  ScreenWidth/375.0

#define kLineColor RGB(229,229,229)
#define kMainPageColor RGBA(0,0,0,0.6)
#define kTitleColor RGB(128,128,128)

CGFloat const NXSharedViewTipLabelHeight = 20;
CGFloat const NXSharedViewUmengViewHeight = 116;
CGFloat const NXSharedViewLineViewHeight = 0.5;
CGFloat const NXSharedViewTopMargin = 10;
CGFloat const NXSharedViewAnimationTime = 0.25;
NSInteger const NXSharedViewColcount = 4; /// 每行显示个数
NSString const * NXSharedViewImageBundle = @"NXShareViewKit";

@interface NXSharedView ()

@property (nonatomic, strong) UIView *backgroundView;//分享白色页面
@property (nonatomic, strong) UIView *bottomLineView;//取消按钮上面的线
@property (nonatomic, strong) UIView *topLineView;//收藏上面的线
@property (nonatomic, strong) UILabel *tipLabel;//分享到
@property (nonatomic, strong) UIView *umengView; // 友盟分享的view
@property (nonatomic, strong) UIView *customView; // 自定义的收藏、举报等视图

@property (nonatomic, strong) NXSharedButton *collectButton;//收藏按钮
@property (nonatomic, strong) NXSharedButton *copyrightButton;//已有版权按钮
@property (nonatomic, strong) NXSharedButton *reportButton;//已举报按钮
@property (nonatomic, strong) UIButton *cancleButton;//取消按钮

@property (nonatomic, assign) CGFloat backgroundViewHeight;
@property (nonatomic, assign) CGFloat cancleBtnHeight;//取消按钮的高度
@property (nonatomic, assign) CGFloat sharedBtnWidth;
@property (nonatomic, assign) CGFloat sharedBtnHeight;

@property (nonatomic, copy) NSArray *section1;
@property (nonatomic, copy) NSArray *section2;

@property (nonatomic, assign) NXSharedViewStyle style;

@end

@implementation NXSharedView

- (instancetype)initWithStyle:(NXSharedViewStyle)style {
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        
        self.style = style;
        [self setupModel];
        [self setHeight];
        [self setupUI];
        [self p_enterAnimation];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(s_cancleButtonClick)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)setHeight{
    
    self.backgroundViewHeight = (self.style == NXSharedViewStyleDefault || self.style == NXSharedViewStyleLittle) ? 200 * kScale : (NXSharedViewUmengViewHeight + 200) * kScale;
    CGFloat height1 = self.backgroundViewHeight - (NXSharedViewTopMargin + NXSharedViewTipLabelHeight + NXSharedViewUmengViewHeight) * kScale;
    CGFloat height2 = self.backgroundViewHeight - (NXSharedViewTopMargin + NXSharedViewTipLabelHeight + NXSharedViewUmengViewHeight * 2) * kScale;
    self.cancleBtnHeight = (self.style == NXSharedViewStyleDefault || self.style == NXSharedViewStyleLittle) ? height1 : height2;
    self.sharedBtnWidth = (self.width / NXSharedViewColcount);
    self.sharedBtnHeight = NXSharedViewUmengViewHeight * kScale;
}

- (void)setupUI {
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    self.backgroundColor = kMainPageColor;
    [self addSubview:self.backgroundView];
//    [self.backgroundView addSubview:self.tipLabel];
    [self.backgroundView addSubview:self.umengView];
    
    if (self.style == NXSharedViewStyleDefault) {
        [self.backgroundView addSubview:self.tipLabel];
        [self p_createSharedBtnsInView:self.umengView section:self.section1];
    }
    if (self.style == NXSharedViewStyleLittle) {
//        [self p_createSharedBtnsInView:self.umengView section:self.section2];
        [self.umengView addSubview:self.collectButton];
        [self.umengView addSubview:self.reportButton];
    }
    if (self.style == NXSharedViewStyleLittleMore) {
        [self.backgroundView addSubview:self.tipLabel];
        [self p_createSharedBtnsInView:self.umengView section:self.section1];
        [self.backgroundView addSubview:self.customView];
        [self.customView addSubview:self.topLineView];
        [self.customView addSubview:self.collectButton];
        //        [self.customView addSubview:self.copyrightButton];
        [self.customView addSubview:self.reportButton];
    }
    if (self.style == NXSharedViewStyleMore) {
        [self.backgroundView addSubview:self.tipLabel];
        [self p_createSharedBtnsInView:self.umengView section:self.section1];
        [self.backgroundView addSubview:self.customView];
        [self.customView addSubview:self.topLineView];
        [self.customView addSubview:self.collectButton];
        [self.customView addSubview:self.copyrightButton];
        [self.customView addSubview:self.reportButton];
    }
    [self.backgroundView addSubview:self.cancleButton];
    [self.cancleButton addSubview:self.bottomLineView];

}

- (void)setupModel {
    
    NXSharedModel *model01 = [NXSharedModel modelWithIcon:@"btn_wechat" tip:@"微信好友" sharedType:NXSharedTypeWechatFriend action:@selector(s_sharedBtnClick:)];
    NXSharedModel *model02 = [NXSharedModel modelWithIcon:@"btn_wechatcircle" tip:@"朋友圈" sharedType:NXSharedTypeTimeLine action:@selector(s_sharedBtnClick:)];
    NXSharedModel *model03 = [NXSharedModel modelWithIcon:@"btn_qq" tip:@"QQ" sharedType:NXSharedTypeQQ action:@selector(s_sharedBtnClick:)];
    NXSharedModel *model04 = [NXSharedModel modelWithIcon:@"btn_sina" tip:@"新浪微博" sharedType:NXSharedTypeWeibo action:@selector(s_sharedBtnClick:)];
    self.section1 = @[model01,model02,model03,model04];
    if (self.style == NXSharedViewStyleLittle) {
        self.section2 = @[model03,model04];
        [self p_createSharedBtnsInView:self.umengView section:self.section2];
    }
}

#pragma mark - handle event 
- (void)s_sharedBtnClick:(NXSharedButton *)button {
    if ([self.delegate respondsToSelector:@selector(sharedViewUMengBtnClick:sharedType:)]) {
        [self.delegate sharedViewUMengBtnClick:self sharedType:button.sharedType];
    }
}

- (void)s_collectionBtnClick:(NXSharedButton *)button {
    button.selected = !button.selected;
    if ([self.delegate respondsToSelector:@selector(sharedViewCollectionBtnClick:)]) {
        [self.delegate sharedViewCollectionBtnClick:self];
    }
}

- (void)s_copyrightBtnClick:(NXSharedButton *)button {
    if ([self.delegate respondsToSelector:@selector(sharedViewCopyrightBtnClick:)]) {
        [self.delegate sharedViewCopyrightBtnClick:self];
    }
}

- (void)s_reportBtnClick:(NXSharedButton *)button {
    if ([self.delegate respondsToSelector:@selector(sharedViewReportBtnClick:)]) {
        [self.delegate sharedViewReportBtnClick:self];
    }
}

- (void)s_cancleButtonClick {
    
    [UIView animateWithDuration:NXSharedViewAnimationTime animations:^{
        self.backgroundView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        self.alpha = 0;
        [self removeFromSuperview];
    }];
}

- (void)dismiss {
    [self s_cancleButtonClick];
}

- (void)updateCollectionBtnState:(BOOL)isCollect {
    self.collectButton.selected = isCollect;
}

- (void)updateCopyrightBtntate:(BOOL)isCopyright {
    self.collectButton.enabled = !isCopyright;
}

#pragma mark - private method
- (void)p_createSharedBtnsInView:(UIView *)view section:(NSArray *)section {
    
    for (NSInteger i = 0; i < section.count; i++) {
        CGRect frame = CGRectMake(i * self.sharedBtnWidth, 0, self.sharedBtnWidth, self.sharedBtnHeight);
        NXSharedModel *model = section[i];
        NXSharedButton *button = [NXSharedButton buttonWithType:UIButtonTypeCustom];
        button.frame = frame;
        [button setTitle:model.tip forState:UIControlStateNormal];
        UIImage *img = [UIImage ff_imagePathWithName:model.icon bundle:NXSharedViewImageBundle targetClass:[self class]];
        [button setImage:img forState:UIControlStateNormal];
        button.sharedType = model.sharedType;
        [button addTarget:self action:@selector(s_sharedBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        
    }
}

- (UIView *)p_createLineViewWithFrame:(CGRect)frame{
    UIView *lineView = [[UIView alloc] initWithFrame:frame];//微信好友下面的线
    lineView.backgroundColor = kLineColor;
    return lineView;
}

- (void)p_enterAnimation {
    
    [UIView animateWithDuration:NXSharedViewAnimationTime animations:^{
        self.backgroundView.transform = CGAffineTransformMakeTranslation(0, -self.backgroundViewHeight);
    }];
}

#pragma mark - getters
- (UIView *)backgroundView {
    if (_backgroundView == nil) {
        CGRect frame = CGRectMake(0, self.height, self.width, self.backgroundViewHeight);
        _backgroundView = [[UIView alloc] initWithFrame:frame];
        _backgroundView.backgroundColor = [UIColor whiteColor];
    }
    return _backgroundView;
}

- (UILabel *)tipLabel {
    if (_tipLabel == nil) {
        CGRect frame = CGRectMake(0, NXSharedViewTopMargin * kScale, self.width, NXSharedViewTipLabelHeight * kScale);
        _tipLabel = [[UILabel alloc] initWithFrame:frame];
        _tipLabel.text = @"分享到";
        _tipLabel.textColor = kTitleColor;
        _tipLabel.font = [UIFont systemFontOfSize:16 * kScale];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _tipLabel;
}

- (UIView *)umengView {
    if (_umengView == nil) {
        CGRect frame;
        if (self.style == NXSharedViewStyleLittle) {
             frame = CGRectMake(0, NXSharedViewTopMargin * kScale, self.width, NXSharedViewUmengViewHeight * kScale);
        } else {
             frame = CGRectMake(0, CGRectGetMaxY(self.tipLabel.frame), self.width, NXSharedViewUmengViewHeight * kScale);}
        _umengView = [[UIView alloc]initWithFrame:frame];
    }
    return _umengView;
}

- (UIView *)customView {
    if (_customView == nil) {
        CGRect frame = CGRectMake(0, CGRectGetMaxY(self.umengView.frame), self.width, self.umengView.height);
        _customView = [[UIView alloc] initWithFrame:frame];
    }
    return _customView;
}

- (NXSharedButton *)collectButton {
    if (_collectButton == nil) {
        CGRect frame = CGRectMake(0, 0, self.sharedBtnWidth, self.sharedBtnHeight);
        _collectButton = [NXSharedButton buttonWithType:UIButtonTypeCustom];
        _collectButton.frame = frame;
        [_collectButton setTitle:@"收藏" forState:UIControlStateNormal];
        [_collectButton setTitle:@"已收藏" forState:UIControlStateSelected];
        UIImage *imgNor = [UIImage ff_imagePathWithName:@"btn_collect_nor" bundle:NXSharedViewImageBundle targetClass:[self class]];
        UIImage *imgSel = [UIImage ff_imagePathWithName:@"btn_collect_h" bundle:NXSharedViewImageBundle targetClass:[self class]];
        [_collectButton setImage:imgNor forState:UIControlStateNormal];
        [_collectButton setImage:imgSel forState:UIControlStateSelected];
        _collectButton.titleLabel.font = [UIFont systemFontOfSize:14 * kScale];
        [_collectButton addTarget:self action:@selector(s_collectionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _collectButton;
}

- (NXSharedButton *)copyrightButton {
    if (_copyrightButton == nil) {
        CGRect frame = CGRectMake(self.sharedBtnWidth*2, 0, self.sharedBtnWidth, self.sharedBtnHeight);
        _copyrightButton = [NXSharedButton buttonWithType:UIButtonTypeCustom];
        _copyrightButton.frame = frame;
        [_copyrightButton setTitle:@"申购版权" forState:UIControlStateNormal];
        [_copyrightButton setTitle:@"已有版权" forState:UIControlStateDisabled];
        UIImage *imgNor = [UIImage ff_imagePathWithName:@"btn_copyright_nor" bundle:NXSharedViewImageBundle targetClass:[self class]];
        UIImage *imgSel = [UIImage ff_imagePathWithName:@"btn_collect_h" bundle:NXSharedViewImageBundle targetClass:[self class]];
        [_copyrightButton setImage:imgNor forState:UIControlStateNormal];
        [_copyrightButton setImage:imgSel forState:UIControlStateDisabled];
        _copyrightButton.titleLabel.font = [UIFont systemFontOfSize:14 * kScale];
        [_copyrightButton addTarget:self action:@selector(s_copyrightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _copyrightButton;
}

- (NXSharedButton *)reportButton {
    if (_reportButton == nil) {
        CGRect frame = CGRectMake(self.sharedBtnWidth, 0, self.sharedBtnWidth, self.sharedBtnHeight);
        _reportButton = [NXSharedButton buttonWithType:UIButtonTypeCustom];
        _reportButton.frame = frame;
        [_reportButton setTitle:@"举报" forState:UIControlStateNormal];
        UIImage *imgNor = [UIImage ff_imagePathWithName:@"btn_report_nor" bundle:NXSharedViewImageBundle targetClass:[self class]];
        [_reportButton setImage:imgNor forState:UIControlStateNormal];
        _reportButton.titleLabel.font = [UIFont systemFontOfSize:14 * kScale];
        [_reportButton addTarget:self action:@selector(s_reportBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reportButton;
}

- (UIButton *)cancleButton {
    if (_cancleButton == nil) {
        
        CGFloat y = self.backgroundViewHeight - self.cancleBtnHeight;
        CGRect frame = CGRectMake(0, y, self.width, self.cancleBtnHeight);
        _cancleButton = [[UIButton alloc] initWithFrame:frame];
        [_cancleButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancleButton setTitleColor:kTitleColor forState:UIControlStateNormal];
        _cancleButton.titleLabel.font = [UIFont systemFontOfSize:16 * kScale];
        [_cancleButton addTarget:self action:@selector(s_cancleButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancleButton;
}

- (UIView *)topLineView {
    if (_topLineView == nil) {
        CGRect frame = CGRectMake(0,0, self.width,NXSharedViewLineViewHeight * kScale);
        _topLineView = [self p_createLineViewWithFrame:frame];
    }
    return _topLineView;
}

- (UIView *)bottomLineView {
    if (_bottomLineView == nil) {
        CGRect frame = CGRectMake(0,0, self.width,NXSharedViewLineViewHeight * kScale);
        _bottomLineView = [self p_createLineViewWithFrame:frame];
    }
    return _bottomLineView;
}

@end
